{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9a22bd77-6cd4-41ec-a0b8-1140c1b36cb0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import datetime\n",
    "import json\n",
    "import random\n",
    "import time\n",
    "\n",
    "from cubestudio.request.model import Model\n",
    "from cubestudio.request.model_client import Client,init\n",
    "from cubestudio.train.task import Notebook,Project\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1f1bb79c-232e-4e77-9590-5de797a453ba",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初始化验证成功\n"
     ]
    }
   ],
   "source": [
    "\n",
    "HOST = \"http://kubeflow-dashboard.infra/\"\n",
    "token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhZG1pbiJ9.j6-hUMaFYdSIzfc6i6TJ5DaS96Z9I78SrjxAOg-71yE'\n",
    "username='admin'\n",
    "init(host=HOST,username=username,token=token)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e596af1a-852c-4a5e-9963-7a934cafa83e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "image_pull_policy 为修改时多余字段\n",
      "expand 为修改时多余字段\n"
     ]
    }
   ],
   "source": [
    "# 添加一个notebook\n",
    "notebook = Client(Notebook).add_or_update(\n",
    "    name='sdk-test',\n",
    "    project=Client(Project).one(name='public'),\n",
    "    describe='sdk创建notebook，并直接提供给其他平台跳转使用',\n",
    "    images='ccr.ccs.tencentyun.com/cube-studio/notebook-enterprise:jupyter-ubuntu-machinelearning',\n",
    "    volume_mount='kubeflow-user-workspace(pvc):/mnt',\n",
    "    image_pull_policy='Always',\n",
    "    resource_memory='10G',\n",
    "    resource_cpu='10',\n",
    "    resource_gpu='0',\n",
    "    expand=json.dumps({\"root\":\"examples/Bayesian/bayesian_network.ipynb\"})\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2c0458bf-4a04-438f-98f8-53c300049995",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"project\": null,\n",
      "    \"name\": \"sdk-test\",\n",
      "    \"namespace\": \"jupyter\",\n",
      "    \"describe\": \"sdk\\u521b\\u5efanotebook\\uff0c\\u5e76\\u76f4\\u63a5\\u63d0\\u4f9b\\u7ed9\\u5176\\u4ed6\\u5e73\\u53f0\\u8df3\\u8f6c\\u4f7f\\u7528\",\n",
      "    \"images\": \"ccr.ccs.tencentyun.com/cube-studio/notebook-enterprise:jupyter-ubuntu-machinelearning\",\n",
      "    \"working_dir\": \"\",\n",
      "    \"env\": \"\",\n",
      "    \"volume_mount\": \"kubeflow-user-workspace(pvc):/mnt\",\n",
      "    \"resource_memory\": \"10G\",\n",
      "    \"resource_cpu\": \"10\",\n",
      "    \"resource_gpu\": \"0\",\n",
      "    \"status\": \"<a target=_blank style=\\\"color:#008000;\\\" href=\\\"/k8s/web/search/dev/jupyter/sdk-test\\\">Running</a>\",\n",
      "    \"ide_type\": \"jupyter\",\n",
      "    \"image_pull_policy\": \"Always\",\n",
      "    \"node_selector\": \"cpu=true,notebook=true\",\n",
      "    \"expand\": \"{}\"\n",
      "}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001B[33m\u001B[2mSource path:... \u001B[22m/src/cubestudio/train/task.py\u001B[0m\n",
      "\u001B[32m\u001B[2mStarting var:.. \u001B[22mself = <cubestudio.train.task.Notebook object at 0x7f832447de20>\u001B[0m\n",
      "\u001B[2m00:24:35.862527 call       135\u001B[0m     def reset(self):\n",
      "\u001B[2m00:24:35.863136 line       136\u001B[0m         begin_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')\n",
      "\u001B[32m\u001B[2mNew var:....... \u001B[22mbegin_time = '2023-08-30 00:24:35'\u001B[0m\n",
      "\u001B[2m00:24:35.863188 line       137\u001B[0m         url = self.client.req(self.client.path+f\"/reset/{self.id}\")\n",
      "\u001B[32m\u001B[2mNew var:....... \u001B[22murl = 'http://10.101.140.141/frontend/dev/dev_online/notebook'\u001B[0m\n",
      "\u001B[2m00:24:38.056701 line       138\u001B[0m         print(f'notebook reset，等待重启成功')\n",
      "\u001B[2m00:24:38.057026 line       139\u001B[0m         exist_message = []\n",
      "\u001B[32m\u001B[2mNew var:....... \u001B[22mexist_message = []\u001B[0m\n",
      "\u001B[2m00:24:38.057095 line       140\u001B[0m         notebook = self.client.show(self.id)\n",
      "\u001B[32m\u001B[2mNew var:....... \u001B[22mnotebook = <cubestudio.train.task.Notebook object at 0x7f8324270eb0>\u001B[0m\n",
      "\u001B[2m00:24:38.105356 line       142\u001B[0m         while ('Running' not in notebook.status):\n",
      "\u001B[2m00:24:38.105470 line       143\u001B[0m             time.sleep(5)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "notebook reset，等待重启成功\n",
      "启动完成，请求地址：/notebook/jupyter/sdk-test/\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001B[2m00:24:43.106974 line       144\u001B[0m             notebook = self.client.show(self.id)\n",
      "\u001B[32m\u001B[2mModified var:.. \u001B[22mnotebook = <cubestudio.train.task.Notebook object at 0x7f832447d1f0>\u001B[0m\n",
      "\u001B[2m00:24:43.150089 line       142\u001B[0m         while ('Running' not in notebook.status):\n",
      "\u001B[2m00:24:43.150197 line       145\u001B[0m         root = json.loads(notebook.expand).get('root','') if notebook.expand else ''\n",
      "\u001B[32m\u001B[2mNew var:....... \u001B[22mroot = ''\u001B[0m\n",
      "\u001B[2m00:24:43.150250 line       146\u001B[0m         if root:\n",
      "\u001B[2m00:24:43.150294 line       149\u001B[0m             print(f'启动完成，请求地址：{HOST}/notebook/jupyter/{notebook.name}/')\n",
      "\u001B[2m00:24:43.150402 return     149\u001B[0m             print(f'启动完成，请求地址：{HOST}/notebook/jupyter/{notebook.name}/')\n",
      "\u001B[36m\u001B[2mReturn value:.. \u001B[22mNone\u001B[0m\n",
      "\u001B[33m\u001B[2mElapsed time: \u001B[22m00:00:07.287961\u001B[0m\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(json.dumps(notebook.to_dict(),indent=4))\n",
    "\n",
    "# if 'Running' not in notebook.status:\n",
    "notebook.reset()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c5fd24b-09dc-4511-a1c5-75ceec2fa7d5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cube-studio",
   "language": "python",
   "name": "cube-studio"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
